﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Web.Mvc;
using VNWIKI_WEB.DAO;
using VNWIKI_WEB.Models;

namespace VNWIKI_WEB.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {

            HomeDAO home = new HomeDAO();
            List<Category> catList = home.GetCatList();
            List<Album> albumList = home.GetAlbumList();
            DataList dtl = new DataList();
            dtl.CatList = catList;
            dtl.AlbumList = AlbumToAlbumPOJO(home, albumList);
            return View("Index", dtl);
        }

        public ActionResult MyProfile()
        {
            User user = (User)Session["user"];
            if (user != null)
            {
                return View(user);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult MyProfileEdit()
        {
            User user = (User)Session["user"];
            if (user != null)
            {
                return View(user);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }

        }

        [HttpPost]
        public ActionResult MyProfileEdit(User user)
        {
            if (ModelState.IsValid)
            {
                VNWIKIDataContext db = new VNWIKIDataContext();
                User user1 = db.Users.Single(u => u.ID == user.ID);
                user1.Name = user.Name;
                user1.AccountName = user.AccountName;
                user1.Email = user.Email;
                if (user.Password != "")
                    user1.Password = user.Password;
                db.SubmitChanges();
                return RedirectToAction("MyProfile");
            }
            return View(user);
        }

        public ActionResult MyAlbums()
        {
            User user = (User)Session["user"];
            if (user != null)
            {
                HomeDAO home = new HomeDAO();
                List<Album> albumList = home.GetAlbumListByUserID(user.ID);
                List<Category> catList = home.GetCatList();
                DataList dtl = new DataList();
                dtl.AlbumList = AlbumToAlbumPOJO(home, albumList);
                dtl.CatList = catList;

                return View("MyAlbums", dtl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult GetAlbumList(string catID)
        {
            HomeDAO home = new HomeDAO();
            List<Album> albumList = home.GetAlbumListByID(Convert.ToInt32(catID));
            List<Category> catList = home.GetCatList();
            Category cat = home.GetCatByID(Convert.ToInt32(catID));
            ViewData["catName"] = cat.Name;
            DataList dtl = new DataList();
            dtl.AlbumList = AlbumToAlbumPOJO(home, albumList);
            dtl.CatList = catList;
            return View("AlbumList", dtl);
        }

        public List<AlbumPOJO> AlbumToAlbumPOJO(HomeDAO home, List<Album> albumList)
        {
            List<AlbumPOJO> albumPJList = new List<AlbumPOJO>();
            foreach (var pt in albumList)
            {
                if (home.GetPhotoList(pt.ID).Count > 0)
                {
                    AlbumPOJO albumPJ = new AlbumPOJO();
                    albumPJ.Id = pt.ID;
                    albumPJ.Name = pt.Name;
                    albumPJ.Vote = (double)pt.Vote;
                    albumPJ.PhotoNum = home.GetPhotoList(pt.ID).Count;
                    albumPJ.UserName = home.GetUserByID((int)pt.UserID).Name;
                    albumPJ.Image = home.GetPhotoList(pt.ID)[0].Link;
                    albumPJ.CatName = home.GetCatByID((int)pt.CategoryID).Name;
                    albumPJ.CatId = (int)pt.CategoryID;
                    albumPJList.Add(albumPJ);
                }
            }
            ViewData["albumNum"] = albumPJList.Count;
            return albumPJList;
        }

        //route abc chạy cái action này
        public ActionResult Views(string albumName, string index)
        {
            HomeDAO home = new HomeDAO();
            ViewData["index"] = index;
            Album album = home.GetAlbumByName(albumName.Replace("-", " "));
            List<Photo> photoList = home.GetPhotoList(album.ID);
            List<Category> catList = home.GetCatList();
            ViewData["photoNum"] = photoList.Count();

            ViewData["albumName"] = album.Name;
            //ViewData["albumName"] = Request.Url;
            ViewData["catName"] = home.GetCatByID((int)album.CategoryID).Name;
            ViewData["catID"] = home.GetCatByID((int)album.CategoryID).ID;
            ViewData["views"] = album.Views;
            Photo photo = photoList[Convert.ToInt32(index)];
            ViewData["albumID"] = album.ID;
            if (CheckIDView(album.ID))
                home.UpdatingViews(album.ID);
            DataList dtl = new DataList();
            dtl.TagsList = home.GetListTags(album.Tags);
            dtl.CatList = catList;
            dtl.Photo = photo;
            return View("Details", dtl);
        }

        public ActionResult AlbumView(string album_id, string index)
        {
            HomeDAO home = new HomeDAO();
            ViewData["index"] = index;
            Album album = home.GetAlbumByID(Convert.ToInt32(album_id));
            List<Photo> photoList = home.GetPhotoList(album.ID);
            List<Category> catList = home.GetCatList();
            ViewData["photoNum"] = photoList.Count();

            ViewData["albumName"] = album.Name;
            //ViewData["albumName"] = Request.Url;
            ViewData["catName"] = home.GetCatByID((int)album.CategoryID).Name;
            ViewData["catID"] = home.GetCatByID((int)album.CategoryID).ID;
            ViewData["views"] = album.Views;
            Photo photo = photoList[Convert.ToInt32(index)];
            ViewData["albumID"] = album.ID;
            if (CheckIDView(album.ID))
                home.UpdatingViews(album.ID);
            DataList dtl = new DataList();
            dtl.TagsList = home.GetListTags(album.Tags);
            dtl.CatList = catList;
            dtl.Photo = photo;
            return View("Details", dtl);
        }

        //Xem hình sử dụng AJAX
        //public PartialViewResult ViewAjax(int albumID, int index)
        //{
        //    HomeDAO home = new HomeDAO();
        //    ViewData["index"] = index;
        //    List<Photo> photoList = home.GetPhotoList(albumID);
        //    List<Category> catList = home.GetCatList();
        //    ViewData["photoNum"] = photoList.Count();
        //    Album album = home.GetAlbumByID(albumID);
        //    ViewData["albumName"] = album.Name;
        //    ViewData["catName"] = home.GetCatByID((int)album.CategoryID).Name;
        //    ViewData["catID"] = home.GetCatByID((int)album.CategoryID).ID;
        //    ViewData["views"] = album.Views;
        //    Photo photo = photoList[index];
        //    ViewData["albumID"] = albumID;
        //    if (CheckIDView(albumID))
        //        home.UpdatingViews(albumID);
        //    DataList dtl = new DataList();
        //    dtl.TagsList = home.GetListTags(album.Tags);
        //    dtl.CatList = catList;
        //    dtl.Photo = photo;
        //    return PartialView("_Image", dtl);
        //}

        public ActionResult Search(string key)
        {
            HomeDAO home = new HomeDAO();
            DataList dtl = new DataList();
            List<Album> albumList = home.Search(key);
            dtl.CatList = home.GetCatList();
            dtl.AlbumList = AlbumToAlbumPOJO(home, albumList);
            ViewData["albumNum"] = albumList.Count;
            ViewData["key"] = key;
            return View("AlbumList", dtl);
        }

        public bool CheckIDView(int albumID)
        {
            bool rs = false;
            string viewID = "views" + albumID.ToString();
            if (Request.Cookies[viewID] == null)
            {
                HttpCookie cookie = new HttpCookie(viewID);
                cookie.Value = viewID;
                cookie.Expires = DateTime.Now.AddHours(3);
                Response.Cookies.Add(cookie);
                rs = true;
            }
            return rs;
        }
    }
}
